package com.suray.wcs.service.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.suray.commin.core.utils.StringUtils;
import com.suray.wcs.service.enums.TaskStatus;
import com.suray.wcs.service.system.entity.AssignConfigDB;
import com.suray.wcs.service.system.entity.PortDB;
import com.suray.wcs.service.system.entity.PortTaskConfigDB;
import com.suray.wcs.service.system.entity.TaskDB;
import com.suray.wcs.service.system.mapper.PortTaskConfigDBMapper;
import com.suray.wcs.service.system.service.IPortTaskConfigDBService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class PortTaskConfigDBServiceImpl extends ServiceImpl<PortTaskConfigDBMapper, PortTaskConfigDB>
        implements IPortTaskConfigDBService {
    @Autowired
    private PortTaskConfigDBMapper portTaskConfigDBMapper;

    public List<PortTaskConfigDB> findConfigByPort(int portId) {
        LambdaQueryWrapper<PortTaskConfigDB> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(PortTaskConfigDB::getPortId, portId);
        return list(lambdaQueryWrapper);
    }
    //分页查询
    @Override
    public List<PortTaskConfigDB> selectList(PortTaskConfigDB portTaskConfigDB) {
        QueryWrapper<PortTaskConfigDB> queryWrapper = new QueryWrapper<>();
        if (portTaskConfigDB.getPortId() != null) {
            queryWrapper.lambda().like(PortTaskConfigDB::getPortId, portTaskConfigDB.getPortId());
        }
        if (portTaskConfigDB.getLayer() != null) {
            queryWrapper.lambda().eq(PortTaskConfigDB::getLayer, portTaskConfigDB.getLayer());
        }
        if (portTaskConfigDB.getOutTaskNum() != null) {
            queryWrapper.lambda().eq(PortTaskConfigDB::getOutTaskNum, portTaskConfigDB.getOutTaskNum());
        }
        return portTaskConfigDBMapper.selectList(queryWrapper);
    }
    //插入
    @Override
    public boolean insertPortConfig(PortTaskConfigDB portTaskConfigDB) {
        if (portTaskConfigDB != null) {
            return this.save(portTaskConfigDB);
        } else {
            return false;
        }
    }

    @Override
    public PortTaskConfigDB getPortConfigByFloorAndPortId(Integer floor, Integer portId) {
        LambdaQueryWrapper<PortTaskConfigDB> eq = new QueryWrapper<PortTaskConfigDB>().lambda();
        eq.eq(PortTaskConfigDB::getLayer, floor);
        eq.eq(PortTaskConfigDB::getPortId, portId);
        List<PortTaskConfigDB> portConfigDBList = this.list(eq);
        if (CollectionUtils.isNotEmpty(portConfigDBList)) {
            return portConfigDBList.get(0);
        }
        return null;
    }

    public PortTaskConfigDB findConfigById(int id) {
        LambdaQueryWrapper<PortTaskConfigDB> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(PortTaskConfigDB::getId, id);
        return list(lambdaQueryWrapper).get(0);
    }


    //删除
    @Override
    public boolean deletePortConfig(List<Integer> ids) {
        if (ids != null) {
            for (Integer i : ids) {
                this.removeById(i);
            }
            return true;
        }
        return false;
    }
    //修改
    @Override
    public boolean updatePortConfig(PortTaskConfigDB portTaskConfigDB) {
        if (portTaskConfigDB != null) {
            return this.update(Wrappers.<PortTaskConfigDB>lambdaUpdate()
                    .set(PortTaskConfigDB::getLayer, portTaskConfigDB.getLayer()) //把email设置成null
                    .set(PortTaskConfigDB::getPortId, portTaskConfigDB.getPortId()) //把email设置成null
                    .set(PortTaskConfigDB::getOutTaskNum, portTaskConfigDB.getOutTaskNum())
                    .eq(PortTaskConfigDB::getId, portTaskConfigDB.getId()));
        } else {
            return false;
        }
    }
}
